﻿using System;
using System.Collections;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using UFSoft.UBF.Business;
using UFSoft.UBF.PL;

namespace UFIDA.U9.Cust.STR.SyncSV.Public
{
    public static class PublicMethod
    {
        public static string StringJoin(IEnumerable<string> args)
        {
            return string.Join(",", args.Select(t => $"'{t}'"));
        }
        public static DateTime GetBirthday(string birthdayStr,string IDCard)
        {
            try
            {
                if (!string.IsNullOrEmpty(birthdayStr))
                    return Convert.ToDateTime(birthdayStr);
                else if (!string.IsNullOrEmpty(IDCard))
                {
                    return Convert.ToDateTime(IDCard.Substring(6, 8));
                }
                else
                    return new DateTime(2000, 1, 1);
            }
            catch(Exception _)
            {
                return new DateTime(2000, 1, 1);
            }
        }
        /// <summary>
        /// U9实体批量in条件查询
        /// </summary>
        /// <typeparam name="T"></typeparam>
        /// <param name="propName"></param>
        /// <param name="codes"></param>
        /// <returns></returns>
        public static List<T> GetDistinctElements<T>(string propName, IEnumerable<string> codes) where T : BusinessEntity
        {
            var elements = new List<T>();
            codes = codes.Distinct();
            if (codes.Any())
            {
                var finder = typeof(T).GetProperty("Finder").GetValue(null);
                var method = finder.GetType().GetMethod("FindAll", new Type[] { typeof(string), typeof(OqlParam[]) });
                string query = $"{propName} in ({StringJoin(codes)})";
                elements = ((IEnumerable)method.Invoke(finder, new object[] { query, new OqlParam[0] })).Cast<T>().ToList();
            }
            return elements;
        }
    }
}
